//
//  Status.swift
//  微博项目
//
//  Created by zhaoyinpeng on 16/3/14.
//  Copyright © 2016年 zhaoyinpeng. All rights reserved.
//

import UIKit
import AFNetworking
class Status: NSObject {

    //.微博创建时间
    var created_at: String?
    
    var id: Int64 = 0
    
    //.微博正文
    var text: String?
    //.微博来源
    var source: String?
    
    var user: User?
    
    var retweeted_status: Status?
    //.配图字典数组
    var pic_urls: [[String : String]]? {
        didSet {
            //.给imageURLs赋值
            guard let urls = pic_urls else {
                //.如果pic_urls为空  直接不给赋值
                return
            }
            
            //.实例化属性
            imageURLs = [NSURL]()
            for item in urls {
                let urlString = item["thumbnail_pic"]
                let url = NSURL(string: urlString!)!
                imageURLs?.append(url)
            }
        }
    }
    
    //.视图层 需要用什么数据 在模型层 就直接处理好 外界直接使用就行啦
    var imageURLs: [NSURL]?
    
    init(dict: [String : AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    
    override func setValue(value: AnyObject?, forKey key: String) {
        //.需要对user 做特殊处理
        if key == "user" {
            guard let dict = value as? [String : AnyObject] else {
                //.不能转换为字典类型
                return
            }
            //.字典转模型   
            user = User(dict: dict)
            //.必须要加 return   不然字典转模型就又转成字典了
            return
            
        }
        if key == "retweeted_status" {
            guard let dict = value as? [String : AnyObject] else {
                //.不能转换为字典类型
                return
            }
            //.字典转模型
            retweeted_status = Status(dict: dict)
            //.必须要加 return   不然字典转模型就又转成字典了
            return
            
        }

        super.setValue(value, forKey: key)
    }
    
    //.过滤
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}

    //.MARK:重写描述方法
    override var description: String {
        let keys = ["id","text","created_at","source"]
        let dict = self.dictionaryWithValuesForKeys(keys)
        
        return dict.description
    }
    
    
}



//.网络访问
extension Status {
    
    
    class func loadHomeData(finished: (array: [Status]?) -> ()) {
        
        guard let token = UserAccount.shareAccout.access_token else {
            print("用户为登录，或者登录过期")
            return
        }
        
        
        
        
        let urlSring:String = "https://api.weibo.com/2/statuses/home_timeline.json"
        let parameters = ["access_token" : token]
        //.实例化网络求情对象
        let AFN = AFHTTPSessionManager()
        
        //.发起get请求
        AFN.GET(urlSring, parameters: parameters, progress: nil, success: { (_, result) -> Void in
            print(result)
            //转换为字典类型
            //.极大的减少分值嵌套
            guard let dict = result as? [String : AnyObject] else {
               finished(array: nil)
                print("数据结构错误")
                return
            }
            
            guard let array = dict["statuses"] as? [[String : AnyObject]] else {
                finished(array: nil)

                print("数据结构错误")
                return
            }
            
            //.实例化临时数组
            var tempArray = [Status]()
            
            //.遍历数组
            for item in array {
                //.字典转模型
               let s = Status(dict: item)
//                print(s)
                //.添加模型数据
                tempArray.append(s)
                
                
            }
            
            //.刷新页面
            finished(array: tempArray)
            }) { (_, error) -> Void in
                finished(array: nil)

                print(error)
        }
    }
    
    
    
    
    
    
    
    
    
    
    
}